<!--#include file="header.html"-->
<div class="layui-row" style="margin-top: 80px;">
    <div class="layui-col-md4 layui-col-md-offset4">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend style="font-size: 14px">登录系统</legend>
        </fieldset>
        <div class="grid-demo grid-demo-bg1">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" lay-verify="title" autocomplete="off" 
                        placeholder="请输入您的用户名称" class="layui-input" id="username" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">登录密码</label>
                    <div class="layui-input-block">
                        <input type="password" name="password" lay-verify="title" autocomplete="off" 
                        placeholder="请您的登录密码" class="layui-input" id="password" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="button" class="layui-btn" lay-submit="" lay-filter="demo1" id="btn_login">登录系统</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<!--#include file="footer.html"-->
<script>
layui.use(['layer', 'jquery'], function() { 
    var $ = jQuery;
    var api_url = get_api_url("index/login");
    var username = $("#username"), password = $("#password");
    $(document).on('click', '#btn_login', function() { 
        var name = username.val(), pass = password.val();
        if (name === "" || pass === "") { 
            layer.alert("请输入用户名称/登录密码");
            return;
        }
        $.ajax({
            url: api_url,
            data: JSON.stringify({name: name, password: pass}),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            type: "POST",
            success: function(res) { 
                if (res.code !== 0) { 
                    layer.alert(res.message);
                    return;
                }
                layer.alert("登录成功", function() {
                    var auth_string = res.result.token;
                    window.localStorage.setItem('authorization', auth_string);
                    top.location.href = '/user.html';
                });
            }
        });
    });
});
</script>
